package com.taotao.manage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.taotao.common.bean.EasyUIResult;
import com.taotao.manage.pojo.Content;
import com.taotao.manage.pojo.Item;
import com.taotao.manage.service.ContentService;

@Controller
@RequestMapping("/content")
public class ContentController {
	@Autowired
	private ContentService contentService;
	/*
	 * $.post("/rest/content",$("#contentEditForm").serialize(), function(data){
				if(data.status == 200
				1.异步请求2.post请求3.返回200
				
	 */
	@RequestMapping(method=RequestMethod.POST)
public ResponseEntity<Void> saveContentController(Content record){
		try {
			record.setId(null);
			this.contentService.save(record);
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	
}
	/**
	 * 
	 * 分页查询
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryContentList(
			@RequestParam(value="categoryId") Long categoryId,
			@RequestParam(value="page",defaultValue="1")Integer pageNum,
			@RequestParam(value="rows",defaultValue="10")Integer pageSize
			){
		try {
			Content record = new Content();
			record.setCategoryId(categoryId);
			//分页查询和排序
			PageInfo<Content> pageInfo = this.contentService.queryPageListByWhere(record , pageNum, pageSize);
			//判断
			if (pageInfo==null || pageInfo.getSize()==0) {
				//没有404
				return  ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			//如果有,分装bean返回 
			EasyUIResult uiResult = new EasyUIResult(pageInfo.getTotal(), pageInfo.getList());
			return ResponseEntity.ok(uiResult);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//500异常
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
}
